

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.CarBarrier;
import com.loong.loong.hik.service.CarBarrierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 道闸
 *
 * @author jinyiang
 * @date 2023-03-30 14:09:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/carbarrier" )
@Api(value = "carbarrier", tags = "道闸管理")
public class CarBarrierController {

    private final  CarBarrierService carBarrierService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param carBarrier 道闸
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_carbarrier_view')" )
    public R getCarBarrierPage(Page page, CarBarrier carBarrier) {

		return R.ok(carBarrierService.page(page,
				Wrappers.query(carBarrier).lambda()
						.like(StringUtils.isNotEmpty(carBarrier.getLikeName()), CarBarrier::getBarrierLocation,carBarrier.getLikeName())
						.between(StringUtils.isNotEmpty(carBarrier.getStartTime())&&StringUtils.isNotEmpty(carBarrier.getEndTime()),CarBarrier::getCreateTime,carBarrier.getStartTime(),carBarrier.getEndTime())));

    }


    /**
     * 通过id查询道闸
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_carbarrier_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(carBarrierService.getById(id));
    }

    /**
     * 新增道闸
     * @param carBarrier 道闸
     * @return R
     */
    @ApiOperation(value = "新增道闸", notes = "新增道闸")
    @SysLog("新增道闸" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_carbarrier_add')" )
    public R save(@RequestBody CarBarrier carBarrier) {
        return R.ok(carBarrierService.save(carBarrier));
    }

    /**
     * 修改道闸
     * @param carBarrier 道闸
     * @return R
     */
    @ApiOperation(value = "修改道闸", notes = "修改道闸")
    @SysLog("修改道闸" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_carbarrier_edit')" )
    public R updateById(@RequestBody CarBarrier carBarrier) {
        return R.ok(carBarrierService.updateById(carBarrier));
    }

    /**
     * 通过id删除道闸
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除道闸", notes = "通过id删除道闸")
    @SysLog("通过id删除道闸" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_carbarrier_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(carBarrierService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param carBarrier 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_carbarrier_export')" )
    public List<CarBarrier> export(CarBarrier carBarrier) {

		return carBarrierService.list(
				Wrappers.query(carBarrier).lambda()
						.like(StringUtils.isNotEmpty(carBarrier.getLikeName()), CarBarrier::getBarrierLocation,carBarrier.getLikeName())
						.between(StringUtils.isNotEmpty(carBarrier.getStartTime())&&StringUtils.isNotEmpty(carBarrier.getEndTime()),CarBarrier::getCreateTime,carBarrier.getStartTime(),carBarrier.getEndTime()));
    }

	/**
	 * 控制道闸（0：关闸 1：开闸 3：常开）
	 * @param barrierNo
	 * @param num 2 常闭， 1 开启，0 关闭，3常开
	 * @return R
	 */
	@ApiOperation(value = "控制道闸", notes = "控制道闸")
	@SysLog("控制道闸")
	@GetMapping("/controller" )
	public R controller(@RequestParam String barrierNo, @RequestParam Integer num) {
		return R.ok(carBarrierService.controller(barrierNo,num));
	}
}
